package com.mj.postcode.activity;

import java.util.List;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.postcode.R;
import com.mj.postcode.bean.Postcode;
import com.mj.postcode.service.PostcodeService;
import com.mj.postcode.util.NetUtils;
import com.mj.postcode.util.RegUtil;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 邮编查地址
 * @author zhaominglei
 * @date 2015-11-10
 * 
 */
public class ToaddressActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = ToaddressActivity.class.getSimpleName();
	private PostcodeService postcodeService = new PostcodeService();
	private ImageView goHome;
	private EditText postcodeEdt; //邮政编码输入框
	private String postcode; //邮政编码
	private Button queryBtn; //查询
	private ProgressBar queryProgressBar; //查询进度条
	private TextView resultView; //结果
	private Button appOffersButton; //推荐应用
	private LinearLayout miniAdLinearLayout; //迷你广告
    private AppControl appControl;
    private Access access;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_toaddress);
		init();
	}
	
	private void init() {
		goHome = (ImageView)findViewById(R.id.toaddress_gohome);		
		postcodeEdt = (EditText)findViewById(R.id.toaddress_code_edt);
		queryBtn = (Button)findViewById(R.id.toaddress_query_btn);
		queryProgressBar = (ProgressBar)findViewById(R.id.toaddress_query_progressbar);
		resultView = (TextView)findViewById(R.id.toaddress_result);
		appOffersButton = (Button)findViewById(R.id.toaddress_appOffersButton);
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.toaddress_miniAdLinearLayout);
		
		goHome.setOnClickListener(this);
		queryBtn.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
    	appControl = AppControl.getInstance();
		appControl.init(ToaddressActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		appControl.loadPopAd(ToaddressActivity.this);
		appControl.showPopAd(ToaddressActivity.this, 60 * 1000);
		appControl.showInter(ToaddressActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(ToaddressActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);		
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.toaddress_gohome:
			ToaddressActivity.this.finish();
			break;
			
		case R.id.toaddress_query_btn:
			postcode = postcodeEdt.getText().toString();
			if (postcode == null || postcode.equals("")) {
				Toast.makeText(getApplicationContext(), R.string.toaddress_code_hint, Toast.LENGTH_SHORT).show();
				return;
			}
			if (!RegUtil.checkPostcode(postcode)) {
				Toast.makeText(getApplicationContext(), R.string.toaddress_error1, Toast.LENGTH_SHORT).show();
				return;
			}
			resultView.setText("");
			getAddressInfo();
			break;
			
		case R.id.toaddress_appOffersButton:
			access.openWALL(ToaddressActivity.this);
			break;			
		default:
			break;
		}
	}
	
	private void getAddressInfo() {
		if (NetUtils.isConnected(getApplicationContext())) {
			queryProgressBar.setVisibility(View.VISIBLE);
			new AddressInfoAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onBackPressed() {
		ToaddressActivity.this.finish();
	}
	
	public class AddressInfoAsyncTask extends AsyncTask<String, String, Postcode> {

		@Override
		protected Postcode doInBackground(String... params) {
			return postcodeService.getAddressInfo(getApplicationContext(), null, postcode);
		}

		@Override
		protected void onPostExecute(Postcode result) {
			queryProgressBar.setVisibility(View.GONE);
			if (result != null) {
				List<Postcode> postcodes = result.getPostcodes();
				if (postcodes != null && !postcodes.isEmpty()) {
					StringBuilder sb = new StringBuilder();
					for (Postcode postcode : postcodes) {
						sb.append(postcode.getZipcode()).append(" ");
						sb.append(postcode.getProvince()).append(" ").append(postcode.getCity()).append(" ");
						sb.append(postcode.getDistrict()).append(" ").append(postcode.getAddress()).append(" \n\n");
					}
					resultView.setText(sb.toString());
				} else {
					Toast.makeText(getApplicationContext(), R.string.toaddress_error2, Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(getApplicationContext(), R.string.toaddress_error2, Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}		
}
